﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MSAcademic.v2.Database;

namespace MSAcademic.v2.Infrastructure.Tag.Impl
{
    public class TagCommandImpl: ITagCommand
    {
        public void Create (string name)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var tag = new Database.Tag
                {
                    Name = name
                };

                db.Tags.Add(tag);
                db.SaveChanges();
            }
        }
        public void Edit (int id, string name)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var dbTag = db.Tags.Where(x => x.Id == id).SingleOrDefault();

                if (dbTag == null)
                {
                    return;
                }

                dbTag.Name = name;
                db.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var dbTag = db.Tags.SingleOrDefault(x => x.Id == id);

                if (dbTag == null)
                {
                    return;
                }

                dbTag.Articles.Clear();
                dbTag.Events.Clear();
                db.Tags.Remove(dbTag);
                db.SaveChanges();
            }
        }
    }
}
